package testcases;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import juf.FieldProperty;
import juf.adapter.Adapter;
import juf.adapter.AdapterFactory;

/** Demo of how components can be validated and invalid save-attempts displayed
 * 
 * @author Dale Halliwell
 *
 */
public class AdapterValidationDemo {


	public static void main(String[] args) {
		
		//create a student
		Student testStudent = new Student(001,"Joe","Nobody", new Date(), false, 100);	
		JPanel panel = new JPanel();

		try{
			//create adapter for the firstname field			
			FieldProperty p = new FieldProperty(testStudent.getClass().getField("gpa"), testStudent);
			final Adapter fnameAdapter = AdapterFactory.createAdapter(p);
			JButton savebtn = new JButton("Save");
			savebtn.addActionListener( //anonymouse inner class to handle the button click
				new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent arg0) {
						try {
							fnameAdapter.save();
						} catch (ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					} 
			});
			panel.add((Component) fnameAdapter);
			panel.add(savebtn);
			//now display the form with that adapter
			JFrame testFrame = new JFrame("Test JufForm"); //create the window frame
			testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the window close operation to exit
			testFrame.getContentPane().add(panel); 

			testFrame.pack(); //resize the window to suit the JufForm
			testFrame.setVisible(true); //show the window
		}
		catch (NoSuchFieldException f) {}
		
		
		

	}

}
